(数据库连接)
import ;
import ;
public class ConnectionTest{
public static void main(String args[ ])throws Exception{
(""); //加载数据库驱动
String url="jdbc:mysql://localhost:3306/xsgl";
Connection con=(url,"root","1234");
}
}
EX:P254(登陆界面-1)
import java. sql. * ;
public class DBConnection{
Connection con=null; //数据库连接对象
public DBConnection( ){
try{(""); //加载数据库驱动
con=("jdbc:mysql://localhost:3306/xsgl","root","1234");
}catch(Exception e){
("数据库访问失败"+( ));
}
}
}
EX:P255(登陆界面-2)
import . * ;
import . * ;
import ;
import ;
import ;
import . * ;
public class Login implements ActionListener{
JFrame f;
JLabel l1;
JLabel l2;
JTextField jt1;
JTextField jt2;
JButton an1;
JButton an2;
public Login(){
f=new JFrame();
("学生登陆");
(new FlowLayout() );
(400,300);
l1=new JLabel("请输入学号:");
l2=new JLabel("请输入姓名:");
jt1=new JTextField(15);
jt2=new JTextField(15);
an1=new JButton("登陆");
an2=new JButton("取消");
(this);
(l1);
(jt1);
(l2);
(jt2);
(an1);
(an2);
(300,200);
数据库连接案例 来自淘豆网m.daumloan.com转载请标明出处.