ConnMysql 类
import java.sql.*;
public class ConnMysql {
public static Connection getconn() throws ClassNotFoundException{
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "sa", "");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
Option类
import java.sql.*;
public class Option {
public void InsertData(String str) throws ClassNotFoundException{
Connection conn = ConnMysql.getconn();
if (conn != null){
System.out.println("success!");
}
}
public static void main(String[] args) {
Option opt1 = new Option();
try {
opt1.InsertData("jjj");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}