一、连接数据库
1、下载SQLServer对应的JDBC驱动
可从MySQL官网下载响应的驱动程序
2、将JDBC驱动导入项目中
将驱动jar包导入项目即可
3、测试是否导入成功
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.test; public class Demo1 { //驱动路径 private static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //程序入口函数 public static void main(String[] args) { try { //加载驱动程序 Class.forName(DBDRIVER); } catch (Exception e) { e.printStackTrace(); } } }
|
程序正常运行则证明数据库驱动程序配置成功。
4、创建数据库连接
例如:连接数据库名为:testdatabase
的数据库,数据库登录用户名:sa
,密码:123456
的数据库。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.test; import java.sql.*; public class Demo1 { //驱动路径 private static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //数据库地址 private static final String DBURL = "jdbc:sqlserver://localhost:1434;DataBaseName=testdatabase"; //数据库登录用户名 private static final String DBUSER = "sa"; //数据库用户密码 private static final String DBPASSWORD = "123456"; //数据库连接 public static Connection conn = null; //程序入口函数 public static void main(String[] args) { try { //加载驱动程序 Class.forName(DBDRIVER); //连接数据库 conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD); System.out.println(conn); } catch (Exception e) { e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
|
程序运行能打印出连接信息,则证明数据库连接正确。
二、数据库操作
1、查询数据
例:查询数据库testdatabase
中students
表的数据。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package com.test; import java.sql.*; public class Demo1 { //驱动路径 private static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //数据库地址 private static final String DBURL = "jdbc:sqlserver://localhost:1434;DataBaseName=testdatabase"; //数据库登录用户名 private static final String DBUSER = "sa"; //数据库用户密码 private static final String DBPASSWORD = "123456"; //数据库连接 public static Connection conn = null; //数据库操作 public static Statement stmt = null; //数据库查询结果集 public static ResultSet rs = null; //程序入口函数 public static void main(String[] args) { try { //加载驱动程序 Class.forName(DBDRIVER); //连接数据库 conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD); //实例化Statement对象 stmt = conn.createStatement(); rs = stmt.executeQuery("select * from students"); while(rs.next()){ String Id = rs.getString(1); String Name = rs.getString(2); String Age = rs.getString(3); String Sex = rs.getString(4); System.out.print("学号:"+Id); System.out.print(" 姓名:"+Name); System.out.print(" 年龄:"+Age); System.out.println(" 性别:"+Sex); System.out.println("------------------------------------"); } stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } }
|
2、更新数据
例:向数据库testdatabase
中的students
表添加一条记录。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package com.demo;
import java.sql.*;
public class Demo_sql {
//驱动路径 public static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //数据库地址 public static final String DBURL = "jdbc:sqlserver://localhost:1434;DatabaseName=testdatabase"; //数据库登录用户名 public static final String DBUSER = "sa"; //数据库用户密码 public static final String DBPASSWORD = "123456"; //数据库连接 public static Connection conn = null; //用于执行SQL语句 public static Statement stmt = null; //SQL语句 public static final String SQLSTR = "insert into students values('201701170002','王思','21','女')"; public static void main(String[] args) { try { Class.forName(DBDRIVER); System.out.println("成功连接!"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD); stmt = conn.createStatement();//实例化statement stmt.executeUpdate(SQLSTR); System.out.println("成功添加!"); } catch (SQLException e) { e.printStackTrace(); }finally{ try { stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
}
|