public static void main(String[] args)
{
Connection conn=null;
Statement sta=null;
String sql="insert into userinfo (name) values('aa')";
boolean flag=false;
try{
conn=getConn();
}catch(Exception e){
System.out.println("找不到conn");
e.printStackTrace() ;
}
try {
sta=conn.createStatement();
flag=sta.execute(sql);
} catch (SQLException e) {
System.out.println("找不到statement");
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print("succes");
}
public static Connection getConn()
{
String url = "jdbc:mysql://localhost:3306/test" ;
String username = "root" ;
String password = "910510" ;
Connection conn=null;
try{
//加载MySql的驱动类
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println("找不到驱动程序类 ,加载驱动失败!");
e.printStackTrace() ;
}
try{
conn =
DriverManager.getConnection(url , username , password ) ;
}catch(SQLException se){
System.out.println("数据库连接失败!");
se.printStackTrace() ;
}
return conn;
}
{
Connection conn=null;
Statement sta=null;
String sql="insert into userinfo (name) values('aa')";
boolean flag=false;
try{
conn=getConn();
}catch(Exception e){
System.out.println("找不到conn");
e.printStackTrace() ;
}
try {
sta=conn.createStatement();
flag=sta.execute(sql);
} catch (SQLException e) {
System.out.println("找不到statement");
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print("succes");
}
public static Connection getConn()
{
String url = "jdbc:mysql://localhost:3306/test" ;
String username = "root" ;
String password = "910510" ;
Connection conn=null;
try{
//加载MySql的驱动类
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println("找不到驱动程序类 ,加载驱动失败!");
e.printStackTrace() ;
}
try{
conn =
DriverManager.getConnection(url , username , password ) ;
}catch(SQLException se){
System.out.println("数据库连接失败!");
se.printStackTrace() ;
}
return conn;
}