Java link mysql database operation

java link mysql

First load the jdbc driver of mysql in java

  Class.forName("com.mysql.jdbc.Driver");

Second, establish a connection with the existing database of mysql

   String url = "jdbc:mysql://localhost:3306/mydata?userUnicode=true&characterEncoding=utf-8&useSSL=false"

  Where jdbc is to call the jdbc driver, mysql refers to the database type here is mysql database, localhost refers to the link address, here refers to the local mysql database, 3306 is the port number used by mysql, useUnicode=true&characterEncoding=utf-8 is to display database data The encoding format used when useSSl=false makes the ssl link closed. In higher versions, it must be set whether the ssl is connected, otherwise an error will be reported.

  Strinf user = “root”;  String psaawd = "123456"

  user is the mysql username, passwd is the password

  Connection con = DriverManager.getConnection(url,user,passwd);

  As part of initialization, the DriverManager class attempts to load the driver class referenced in the "jdbc.drivers" system property
  getConnection(String url, String user, String password) The method tries to build to the given database URL Connection

Links to the database have been established since

1
2
3
4
5
6
7
8
9
10
11
12
13
public  static  Connection getConnection ()  throws  SQLException,
         java.lang.ClassNotFoundException
{
     //加载mysql的jdbc驱动
     Class.forName( "com.mysql.jdbc.Driver" );
     //取得连接的url和用户名和密码
     String url =  "jdbc:mysql://localhost:3306/mydate?useUnicode=true&characterEncoding=utf-8&useSSL=false" ;
     String user =  "root" ;
     String passwd =  "123456" ;
     //创建与数据库的实例连接官网:www.fhadmin.org
     Connection con = DriverManager.getConnection(url, user,passwd);
     return  con;
}

 verify:

Create a sql static statement execution object, which can statically execute sql statements

Statement sql = con.createStatement()Statement executes sql statement statically. A Strmaen object can only open one ResultSet object. Therefore, if Statement reads multiple ResultSet objects, there must be multiple Statement objects corresponding to it. If there are Statement objects If the current ResultSet object is opened, other Statements that read the ResultSet object must implicitly close it.

Create new table

sql.executeUpdate("create table person(id int(4) not null,name varchar(20),sex char(1),primary key(id))");
添加新成员
sql.executeUpdate("insert into person(1,"王二","M")");

View the contents of the table

String query = "select * from student";
ResultSet result = sql.executeQuery(query);
while(result.next()){
    
    System.out.println("Id:" + resuli.getInt("id") + "    name:" + result.getString("name") + "    sex:"+ result.getString("sex"));
}

Disconnect

sql.close();
con.close();

  View Code
 
//complete code 
import
java.sql.* ; public class Mysql2{ public static Connection getConnection () throws SQLException, java.lang.ClassNotFoundException { //加载mysql的jdbc驱动 Class.forName("com.mysql.jdbc.Driver"); //取得连接的url和用户名和密码官网:www.fhadmin.org String url = "jdbc:mysql://localhost:3306/mydate?useUnicode=true&characterEncoding=utf-8&useSSL=false"; String user = "root"; String passwd = "123456"; //创建与数据库的实例连接 Connection con = DriverManager.getConnection(url, user,passwd); return con; } public static void main(String[] args) { try { Connection con = getConnection(); Statement sql = con.createStatement(); //如果同名数据库存在就删除官网:www.fhadmin.org //sql.executeUpdate("insert person values(, 'liying',)"); //执行一个sql语句,生成一为student的表 sql.executeUpdate("create table student (id int(20) not null auto_int not nullt, name varchar() not null default 'name', sex char(2) not null default , primary key (id) ); "); sql.executeUpdate("insert into student values(321,'liying','w')"); sql.executeUpdate("insert into student values(123,'wangjiawu','w')"); sql.executeUpdate("insert into student values(132,'Baili','w')"); //查询操作,用ResultSet类,返回结果 String query = "select * from student"; ResultSet result = sql.executeQuery(query); //显示查询内容 System.out.println("Student 表中的内容:"); System.out.println("---------------------------------"); System.out.println(" 序号" + " | " + "姓名" +" | "+ "性别"); System.out.println("--------------------------------"); //对查询结果进行处理 while (result.next()) { int id = result.getInt("id"); String name = result.getString("username"); String mathsorce = result.getString("sex"); System.out.println(" |" + id + " |" + name + " |" + mathsorce); } sql.close();//关闭数据库 con.close();//断开连接 }catch (java.lang.ClassNotFoundException e){ System.out.println("Error"); }catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } } }
 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326223534&siteId=291194637