java如何插入数据进sql
1个回答
展开全部
(最基本的连接方法)
1。获取连接
获取连接需要两步,
一是使用DriverManager来注册驱动(Class.forName(“com.mysql.jdbc.Driver”)),二是使用DriverManager来获取Connection对像DriverManager.getConnection(url,username,password)
2.获取Statement(Statement stmt =con.createStatement();)
Statement就是执行sql语句的;
3.执行sql语句
String sql = “insertinto user value(’zhangSan’, ’123’)”;
int m =stmt.executeUpdate(sql);
//总代码如下
publicstatic Connection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mydb1";
return DriverManager.getConnection(url, "root", "123");
}
@Test
publicvoid insert() throws Exception {
Connection con = getConnection();
Statement stmt = con.createStatement();
String sql = "insert into user values('zhangSan', '123')";
stmt.executeUpdate(sql);
System.out.println("插入成功!");
}
1。获取连接
获取连接需要两步,
一是使用DriverManager来注册驱动(Class.forName(“com.mysql.jdbc.Driver”)),二是使用DriverManager来获取Connection对像DriverManager.getConnection(url,username,password)
2.获取Statement(Statement stmt =con.createStatement();)
Statement就是执行sql语句的;
3.执行sql语句
String sql = “insertinto user value(’zhangSan’, ’123’)”;
int m =stmt.executeUpdate(sql);
//总代码如下
publicstatic Connection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mydb1";
return DriverManager.getConnection(url, "root", "123");
}
@Test
publicvoid insert() throws Exception {
Connection con = getConnection();
Statement stmt = con.createStatement();
String sql = "insert into user values('zhangSan', '123')";
stmt.executeUpdate(sql);
System.out.println("插入成功!");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询