package daily;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBC {
//得到Statement
public static Statement getStatement() throws Exception{
//加载JDBC驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("找不到驱动类,加载驱动失败");
e.printStackTrace();
}
//提供连接数据库的URL
String url = "jdbc:mysql://localhost:3306/project";
String user = "lwj";
String password = "123456";
String sql = "select * from t_user where name=?";
Statement statement = null;
Connection connection = null;
try {
//得到连接
connection = DriverManager.getConnection(url, user, password);
//得到块
//对sql语句执行预处理
PreparedStatement prepareStatement = connection.prepareStatement(sql);
prepareStatement.setString(1, "嘿嘿");
String sql2 = "";
//execute是执行的意思
prepareStatement.executeQuery();
prepareStatement.execute(sql2);
prepareStatement.executeUpdate();
statement = prepareStatement;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭连接
connection.close();
}
return statement;
}
}
JDBC连接
最新推荐文章于 2024-07-10 21:58:42 发布