首页 > 代码库 > 使用Java jdbc 链接mysql数据库过程

使用Java jdbc 链接mysql数据库过程

1.创建工程,下载mysql驱动包添加到classpath;

2.主要步骤:

     a.加载驱动 class.forName("com.mysql.jdbc.Driver");

     b.使用该驱动连接到数据库,参数包括数据库路径,用户名,密码 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/3306/gaoxiangyu","root","root");

     c.获取Statement对象用来执行sql语句:Statement stm = conn.creatStatment();

     d.Statement对象可以执行excutUpdate(sql);//只能实现 增删改  不能实现查

                                            executeQuery(sql);//可以实现查,返回ResultSet结果集

                                            excute(sql);//可以执行任何sql,但返回值只能是boolean类型

 

 1 package com.database;
 2 
 3 import java.sql.*;
 4 public class GetConnection {
 5     public static String Diver = "com.mysql.jdbc.Driver";
 6     public static String DataBase = "jdbc:mysql://localhost:3306/gaoxiangyu";
 7     public static String name = "root";
 8     public static String password = "root";
 9     
10     
11     public static java.sql.Connection getConnection(){
12         try{ 
13             java.sql.Connection conn = null;
14             Class.forName(Diver);//加载驱动
15             conn = DriverManager.getConnection(DataBase,name,password);//根据数据库名、user、password连接到数据库
16             return conn;
17         }catch(Exception e){
18             e.printStackTrace();
19         }
20         return null;
21         
22     }
23 
24     public static void main(String[] args) {
25         // TODO Auto-generated method stub
26 
27     }
28 
29 }

 

 1 package com.database;
 2 import java.sql.*;
 3 public class ExcuteSql {
 4     
 5     public static int Update(String sql,Connection conn) throws SQLException{
 6         Statement st = conn.createStatement();
 7         return st.executeUpdate(sql);//可以执行 增 删 改
 8     }
 9     
10     public static ResultSet search(String sql,Connection conn) throws SQLException{
11         Statement st = conn.createStatement();
12         return st.executeQuery(sql);
13     }
14     
15     public static boolean any(String sql,Connection conn) throws SQLException{
16         Statement st = conn.createStatement();
17         return st.execute(sql);
18         
19     }
20 
21 }

 

 1 package com.database;
 2 
 3 import java.sql.Connection;
 4 import java.sql.ResultSet;
 5 import java.sql.ResultSetMetaData;
 6 import java.sql.SQLException;
 7 
 8 public class DataBaseTest {
 9     public static void main(String args[]) throws SQLException{
10         try(Connection conn = GetConnection.getConnection()){
11             String sql1 = "SELECT * FROM gaoxiangyu.userinfo;";
12             String sql2 = "INSERT INTO gaoxiangyu.userinfo VALUES (7717, \‘wudiyy\‘,\‘root\‘)";
13             ExcuteSql.Update(sql2, conn);
14 //            System.out.println(ExcuteSql.any(sql1, conn));
15             ResultSet result = ExcuteSql.search(sql2, conn);
16              //Retrieves the number, types and properties of this ResultSet object‘s columns.
17             ResultSetMetaData metaData =http://www.mamicode.com/ result.getMetaData();
18             int coulmn = metaData.getColumnCount();
19             while(result.next()){
20                 for(int i=1;i<=coulmn;i++){
21                     System.out.print(result.getString(i)+" ");
22                 }
23                 System.out.println();
24             }
25         }
26     }
27 }

 

使用Java jdbc 链接mysql数据库过程