我正在尝试连接MySQL数据库,但此代码中有一些错误,请帮助我。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MyClass {
static public final String driver = "com.mysql.jdbc.Driver";
static public final String connection ="jdbc:mysql://localhost:3306/ashu";
static public final String user = "root";
static public final String pass= "admin";
ResultSet rs;
Connection con;
Statement smt;
public Connection openConnection(){
try{
Class.forName(driver);
con=DriverManager.getConnection(connection,user,pass);
smt=con.createStatement(rs.TYPE_SCROLL_SENSITIVE,rs.CONCUR_UPDATABLE);
}catch(Exception ex){
System.out.println("Connection Failed: "+ex);
}
return con;
}
public void closeConnection(){
try{
con.close();
}catch(Exception ex){
System.out.println("Closing Connection Failed*****");
}
}
public String getData(String id){
String result="";
openConnection();
try{
rs=smt.executeQuery("select name from mytable where no="+id);
while(rs.next()){
result=rs.getString(1);
}
}catch(Exception ex){
System.out.println("getData() Failed*****");
}
System.out.println("Result*****"+result);
closeConnection();
return result;
}
}
答案 0 :(得分:0)
如果你的目标是Android手机,你最好使用SQLite。
See this tutorial如果要在运行时创建自己的数据库。 或you can follow this如果您拥有自己的数据库,并且想在应用中使用它。
祝你好运!