Java JDBC链接数据库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java JDBC链接数据库相关的知识,希望对你有一定的参考价值。
1.注册驱动
Class.forname("com.mysql.jdbc.Driver");//这是连接mysql数据库的驱动
2.获取数据库连接
java.sql.Connection conn=java.sql.DriverManager.getConnection();
Class.forname("com.mysql.jdbc.Driver");//这是连接mysql数据库的驱动
2.获取数据库连接
java.sql.Connection conn=java.sql.DriverManager.getConnection();
3.获取表达式
java.sql.Statement stmt=conn.createStatement("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK","root","null");//三个参数分别是数据库连接的URL,用户名,密码
java.sql.Statement stmt=conn.createStatement("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK","root","null");//三个参数分别是数据库连接的URL,用户名,密码
4.执行SQL
java.sql.ResultSet rs=stmt.executeQuery("select * from user");
java.sql.ResultSet rs=stmt.executeQuery("select * from user");
5.显示结果集里面的数据
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
System.out.pringln();
}//执行插入语句
//stmt.executeUpdate("insert into user values(1,‘中文‘,‘345‘)");
6.释放资源
rs.close();
stmt.close();
conn.close();
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
System.out.pringln();
}//执行插入语句
//stmt.executeUpdate("insert into user values(1,‘中文‘,‘345‘)");
6.释放资源
rs.close();
stmt.close();
conn.close();
package com.jikexueyuan.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCTest {
public static void main(String[] args) {
String sql="select * from tbl_user";
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_db","root","");
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
System.out.print(rs.getInt("id")+" ");
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getString("password")+" ");
System.out.print(rs.getString("email")+" ");
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
rs.close();
st.close();
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCTest {
public static void main(String[] args) {
String sql="select * from tbl_user";
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_db","root","");
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
System.out.print(rs.getInt("id")+" ");
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getString("password")+" ");
System.out.print(rs.getString("email")+" ");
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
rs.close();
st.close();
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
以上是关于Java JDBC链接数据库的主要内容,如果未能解决你的问题,请参考以下文章