JDBC连接MySQL经典方案
最近在学习数据库开发的一些实例,这里浅谈一下用JDBC连接数据库MySQL(当然也可以连接SQL Sever或Oracle了,只是我更喜欢开源软件,同时也更简单)。
首先正确安装好MySQL,建立好数据库studentinfo
mysql下载appmysql>create database studentinfo;
然后编写java代码,ConnectToMySQL.java
import java.sql.*;
public class ConnectToMySQL {
public static Connection getConnection() throws SQLException ,
public static Connection getConnection() throws SQLException ,
java.lang.ClassNotFoundException{
String url = "jdbc:mysql://localhost:3306/studentinfo";
Class.forName("sql.jdbc.Driver");
String userName = "root";
String password = "";
Connection con = Connection(url,userName,password);
return con;
}
String url = "jdbc:mysql://localhost:3306/studentinfo";
Class.forName("sql.jdbc.Driver");
String userName = "root";
String password = "";
Connection con = Connection(url,userName,password);
return con;
}
public static void main(String[] args) {
try{
Connection con = getConnection();
Statement sql = ateStatement();
&ute("drop table if exists student");
&ute("create table student(id int not null auto_increment,name varchar(20) not nu
ll default 'name',math int not null default 60,primary key(id));");
&ute("insert student values(1,'AAA','99')");
&ute("insert student values(2,'BBB','77')");
&ute("insert student values(3,'CCC','65')");
String query = "select * from student";
ResultSet result = uteQuery(query);
System.out.println("Student表数据如下:");
System.out.println("---------------------------------");
System.out.println("学号"+" "+"姓名"+" "+"数学成绩");
System.out.println("---------------------------------");
int number;
String name;
String math;
()){
number = Int("id");
&ute("insert student values(1,'AAA','99')");
&ute("insert student values(2,'BBB','77')");
&ute("insert student values(3,'CCC','65')");
String query = "select * from student";
ResultSet result = uteQuery(query);
System.out.println("Student表数据如下:");
System.out.println("---------------------------------");
System.out.println("学号"+" "+"姓名"+" "+"数学成绩");
System.out.println("---------------------------------");
int number;
String name;
String math;
()){
number = Int("id");
name = String("name");
math = String("math");
System.out.println(number + " " + name + " " + math);
}
sql.close();
con.close();
}catch(java.lang.ClassNotFoundException e){
&println("ClassNotFoundException:" + e.getMessage());
}catch(SQLException ex){
&println("SQLException:" + ex.getMessage());
}
}
}
math = String("math");
System.out.println(number + " " + name + " " + math);
}
sql.close();
con.close();
}catch(java.lang.ClassNotFoundException e){
&println("ClassNotFoundException:" + e.getMessage());
}catch(SQLException ex){
&println("SQLException:" + ex.getMessage());
}
}
}
要注意的是使用MySQL数据库,需要用到对应的JDBC驱动程序mysql-connector-java-5.0.
3
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.*;
public class BaseConnection {
private Connection con = null;
private Connection con = null;
protected Connection getCon(){
ResultSet rs=null;
Statement stmt = null;
try {
try {
Class.forName("sql.Driver");
String url="jdbc:mysql://192.168.0.10/数据库名?user=USR&password=PWD";
conn = Connection(url);
String url="jdbc:mysql://192.168.0.10/数据库名?user=USR&password=PWD";
conn = Connection(url);
stmt = ateStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
}
1.把 mysql-connector-java-5.0.3-bin.jar放到%JAVA_HOME%\jre\lib\ext下
2.访问类:
2.访问类:
package mysqldb;
import java.sql.*;
public class MysqlCon {
private static String DriverName = "sql.Driver";
private String dbURL = "jdbc:mysql://localhost/test";
private String dbuser = "root";
private String dbpassword = "";
private Connection conn;
private Statement stmt;
private static String DriverName = "sql.Driver";
private String dbURL = "jdbc:mysql://localhost/test";
private String dbuser = "root";
private String dbpassword = "";
private Connection conn;
private Statement stmt;
private ResultSet rs;
public MysqlCon(){
try {
Class.forName(DriverName).newInstance();
conn = Connection(dbURL,dbuser,dbpassword);
stmt = ateStatement();
String sql = "select * from worker";
rs = uteQuery(sql);
()){
System.out.String(1));
}
} catch (InstantiationException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IllegalAccessException e) {
public MysqlCon(){
try {
Class.forName(DriverName).newInstance();
conn = Connection(dbURL,dbuser,dbpassword);
stmt = ateStatement();
String sql = "select * from worker";
rs = uteQuery(sql);
()){
System.out.String(1));
}
} catch (InstantiationException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
package interphase;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import mysqldb.MysqlCon;
public class ShowRS {
private AbstractTableModel atm;
private JTable jtable;
private Vector vector;
private JScrollPane jsp;
private AbstractTableModel atm;
private JTable jtable;
private Vector vector;
private JScrollPane jsp;
private String title[]={"职工号","职工名","性别","出生日期","工资"};
private MysqlCon mysqlcon;
private JFrame frame;
public ShowRS(){
vector = new Vector();
atm = new AbstractTableModel(){
public int getColumnCount() {
return title.length;
}
private MysqlCon mysqlcon;
private JFrame frame;
public ShowRS(){
vector = new Vector();
atm = new AbstractTableModel(){
public int getColumnCount() {
return title.length;
}
public int getRowCount() {
return vector.size();
}
return vector.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
if(!vector.isEmpty())
return ((Vector)vector.elementAt(rowIndex)).elementAt(columnIndex);
else
return null;
}
//取得单元格中的属性值
public String getColumnName(int columnIndex){
return title[columnIndex];
}
//数据模型不可编辑,该方法设置为空
public void setValueAt(){}
//取得列所属对象类
public Class getCoumnClass(int c){
return getValueAt(0,c).getClass();
}
return ((Vector)vector.elementAt(rowIndex)).elementAt(columnIndex);
else
return null;
}
//取得单元格中的属性值
public String getColumnName(int columnIndex){
return title[columnIndex];
}
//数据模型不可编辑,该方法设置为空
public void setValueAt(){}
//取得列所属对象类
public Class getCoumnClass(int c){
return getValueAt(0,c).getClass();
}
//设置单元格不可编辑,为缺省实现
public boolean isCellEditable(int row,int column){
return false;
}};
jtable = new JTable(atm);
jtable.setToolTipText("显示全部查询结果");
jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jtable.setCellSelectionEnabled(false);
jtable.setShowVerticalLines(true);
jtable.setShowHorizontalLines(true);
jsp = new JScrollPane(jtable);
mysqlcon = new MysqlCon();
&veAllElements();
atm.fireTableDataChanged();
try {
public boolean isCellEditable(int row,int column){
return false;
}};
jtable = new JTable(atm);
jtable.setToolTipText("显示全部查询结果");
jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jtable.setCellSelectionEnabled(false);
jtable.setShowVerticalLines(true);
jtable.setShowHorizontalLines(true);
jsp = new JScrollPane(jtable);
mysqlcon = new MysqlCon();
&veAllElements();
atm.fireTableDataChanged();
try {
ResultSet rs = ResultSet();
()){
Vector rec_vector = new Vector();
rec_vector.String(1));
rec_vector.String(2));
rec_vector.String(3));
rec_vector.Date(4));
rec_vector.addElement(new Float(5)));
vector.addElement(rec_vector);
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
atm.fireTableDataChanged();
()){
Vector rec_vector = new Vector();
rec_vector.String(1));
rec_vector.String(2));
rec_vector.String(3));
rec_vector.Date(4));
rec_vector.addElement(new Float(5)));
vector.addElement(rec_vector);
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
atm.fireTableDataChanged();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(jsp);
frame.setSize(400,300);
frame.setVisible(true);
}
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(jsp);
frame.setSize(400,300);
frame.setVisible(true);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论