时间:2021-07-01 10:21:17 帮助过:2人阅读
3.连接数据库
1 public void OpenLik() { 2 try { 3 String url = "jdbc\:mysql\://localhost\:3306/test1"; 4 String username = "root"; 5 String password = "root"; 6 System.out.println(url + username + password); 7 con = DriverManager.getConnection(url, username, password); 8 System.out.println("数据库连接成功!"); 9 } catch (SQLException se) { 10 System.out.println("数据库连接失败!"); 11 // se.printStackTrace(); 12 } 13 }
4.创建数据库执行命令,并传入sql 操作语句
1 public Statement GetSqlStringCommand(String query) { 2 // TODO Auto-generated method stub 3 PreparedStatement pstmt = null; 4 try { 5 pstmt = con.prepareStatement(query); 6 System.out.println("获取命令成功"); 7 } catch (Exception ex) { 8 System.out.println("获取命令失败"); 9 // ex.printStackTrace(); 10 } 11 return pstmt; 12 }
5.根据数据库命令,操作sql命令语句
1 public ResultSet ExecuteDataSet(Statement cmd) { 2 // TODO Auto-generated method stub 3 ResultSet RS = null; 4 PreparedStatement cmd_ = (PreparedStatement) cmd; 5 try { 6 RS = cmd_.executeQuery(); 7 System.out.println("获取ResultSet成功"); 8 } catch (Exception ex) { 9 System.out.println("获取ResultSet失败"); 10 // ex.printStackTrace(); 11 } 12 return RS; 13 }
这样就可以进行数据库的连接了!!!
java 如何连接MySql数据库
标签: