使用java连接MySQL数据库与其他的数据库连接核心是一样的,如果说区别,那就是所需的驱动不一样.
工具/原料
MySQL、JDK
方法/步骤
①.、首先需要安装好JDK(配置环境变量),如图所示:
(1)确定连接路径URL:
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(url)
①. 将数据库的JDBC驱动加载到classpath中,在基于JAVAEE的WEB应用实际开发过程中,通常要把目标数据库产品的JDBC驱动复制到WEB-INF/lib下.
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//Informix数据库
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
//Sybase数据库
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
//MySQL数据库
Class.forName("com.mysql.jdbc.Driver").newInstance();
//PostgreSQL数据库
Class.forNaem("org.postgresql.Driver").newInstance();
String user="scott";
String password="tiger";
Connection conn=DriverManager.getConnection(url,user,password);
--完整的太多了!我已经把完整的代码发到你QQ邮箱了!
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class inensshow extends JFrame {
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData rsMetaData;
//GUI变量定义
private JTable table;
private JTextArea inputQuery;
private JButton submitQuery;
public inensshow()
{
//Form的标题
super( "输入SQL语句,按查询按钮查看结果." );
String username = "inens";
String password = "inens";
//加载驱动程序以连接数据库
try {
Class.forName( "org.gjt.mm.mysql.Driver" );
connection = DriverManager.getConnection(
url, username, password );
}
//捕获加载驱动程序异常
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"装载 JDBC/ODBC 驱动程序失败." );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
//捕获连接数据库异常
catch ( SQLException sqlex ) {
System.err.println( "无法连接数据库" );
sqlex.printStackTrace();
//如果数据库连接成功,则建立GUI
//SQL语句
String test="SELECT * FROM data";
submitQuery = new JButton( "查询" );
//Button事件
submitQuery.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
getTable();
);
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
topPanel.add( new JScrollPane( inputQuery), BorderLayout.CENTER );
//将"提交查询"按钮布置到 "SOUTH"
topPanel.add( submitQuery, BorderLayout.SOUTH );
table = new JTable();
Container c = getContentPane();
c.setLayout( new BorderLayout() );
c.add( topPanel, BorderLayout.NORTH );
c.add( table, BorderLayout.CENTER );
//显示Form
show();
private void getTable()
//执行SQL语句
String query = inputQuery.getText();
statement = connection.createStatement();
resultSet = statement.executeQuery( query );
//在表格中显示查询结果
throws SQLException
//定位到达第一条记录
boolean moreRecords = rs.next();
//如果没有记录,则提示一条消息
if ( ! moreRecords ) {
JOptionPane.showMessageDialog( this,
"结果集中无记录" );
setTitle( "无记录显示" );
return;
Vector columnHeads = new Vector();
Vector rows = new Vector();
//获取字段的名称
ResultSetMetaData rsmd = rs.getMetaData();
for ( int i = 1; i = rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );
//获取记录集
do {
rows.addElement( getNextRow( rs, rsmd ) );
} while ( rs.next() );
table = new JTable( rows, columnHeads );
JScrollPane scroller = new JScrollPane( table );
c.remove(1);
c.add( scroller, BorderLayout.CENTER );
//刷新Table
c.validate();
private Vector getNextRow( ResultSet rs,
ResultSetMetaData rsmd )
Vector currentRow = new Vector();
currentRow.addElement( rs.getString( i ) );
//返回一条记录
return currentRow;
public void shutDown()
//断开数据库连接
connection.close();
System.err.println( "Unable to disconnect" );
public static void main( String args[] )
final inensshow app =
new inensshow();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
app.shutDown();
System.exit( 0 );
------------------------------------------------------------
代码主要列出连接数据库的关键代码,其他访问数据库代码省略
//orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
//sample为你的数据库名
String user="admin";
String password="";
//mydb为数据库
String user="sa";
//myDB为你的数据库名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
String url =
user=testuser;password=testpassword";
//myDB为数据库名
Connection conn= DriverManager.getConnection(url);
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB"
String user="myuser";
String password="mypassword";
xe为本机数据库库名.
private static String driverName="oracle.jdbc.driver.OracleDriver";
这一条是声明一个字符串存储数据库驱动
用这个类吧.好的话,给我加加分.
/**
* @功能: 一个JDBC的本地化API连接类,封装了数据操作方法,只用传一个SQL语句即可
*/
public class ConnectionDemo {
/*
* 这里可以将常量全部放入另一个类中,以方便修改
private static Connection conn;
private static Statement ps;
private static ResultSet rs;
private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String USER ="sa";
private static final String PASS = "sa";
public ConnectionDemo() {
// TODO Auto-generated constructor stub
ConnectionDemo.getConnection();
public static Connection getConnection(){
System.out.println("连接中...");
Class.forName(ConnectionDemo.DRIVER);
conn = DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS);
System.out.println("成功连接");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
return conn;
public static Statement getStatement(String sql){
System.out.println("执行SQL语句中...");
ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = ps.executeQuery(sql);
System.out.println("执行完查询操作,结果已返回ResultSet集合");
ps.executeUpdate(sql);
System.out.println("已执行完毕删除操作");
System.out.println("已执行完毕增加操作");
}else{
System.out.println("已执行完毕更新操作");
return ps;
public static ResultSet getResultSet(){
System.out.println("查询结果为:");
return rs;
public static void closeConnection(){
System.out.println("关闭连接中...");
if (rs != null) {
rs.close();
System.out.println("已关闭ResultSet");
if (ps != null) {
ps.close();
System.out.println("已关闭Statement");
if (conn != null) {
conn.close();
System.out.println("已关闭Connection");
} catch (Exception e) {
// TODO: handle exception
public static void main(String[] args) {
// TODO Auto-generated method stub
String sql = "delete from type where id = 1";
ConnectionDemo.getStatement(sql);
String sql1 = "select * from type";
ConnectionDemo.getStatement(sql1);
ResultSet rs = ConnectionDemo.getResultSet();
System.out.println("编号 "+"类 型");
while(rs.next()){
System.out.print(" "+rs.getInt(1)+" ");
ConnectionDemo.closeConnection();
以上就是土嘎嘎小编为大家整理的java数据库的连接代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!