package train;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
public class Train
{
public String l_search(int m,String line) throws Exception {
String a = null;
String sql = "SELECT * FROM train where line like '%"+line+"%'" ;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/abc";
String username= "root";
String Password= "123123";
Connection connection = DriverManager.getConnection(url, username, Password);
java.sql.Statement stmt = connection.createStatement();
java.sql.ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
a = rs.getString(m);
stmt.close();
rs.close();
return a;
}
public List<String> t_search(String station) throws Exception {
String sql = "SELECT * FROM train" ;
List<String> s=new ArrayList<>();
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/abc";
String username= "root";
String Password= "123123";
Connection connection = DriverManager.getConnection(url, username, Password);
java.sql.Statement stmt = connection.createStatement();
java.sql.ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData() ;
while(rs.next())
{
for(int i=1;i<rsmd.getColumnCount();i++)
{
if(rs.getString(i).equals(station))
s.add(rs.getString(1));
}
}
stmt.close();
rs.close();
return s;
}
public void add(String line,String sta1,String sta2,String sta3,String sta4,String sta5,String sta6,String sta7,String sta8) throws Exception {
String sql = "INSERT into train VALUES('"+line+"','"+sta1+"','"+sta2+"','"+sta3+"','"+sta4+"','"+sta5+"','"+sta6+"','"+sta7+"','"+sta8+"')";
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/abc";
String username = "root";
String Password = "123123";
Connection connection = DriverManager.getConnection(url, username, Password);
java.sql.Statement stmt = connection.createStatement();
stmt.executeUpdate(sql);
stmt.close();
}
}
标签:总结,java,String,rs,mysql,stmt,学习,sql,3.29 From: https://www.cnblogs.com/clh628/p/17277790.html