package test;
import javasql;
import cncomsdatautilSDGetConnection;
/
@author user
mssql数据库连接 *** 作
/
public class MssqlConnTest {
String driverName = "commicrosoftsqlserverjdbcSQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://127001:1433; DatabaseName=test"; //连接服务器和数据库sample
String userName = "sa"; //默认用户名
String userPwd = "000000"; //密码
Connection dbConn;
public Connection getConnection()
{
try
{
ClassforName(driverName);
dbConn = DriverManagergetConnection(dbURL, userName, userPwd);
Systemoutprintln("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!
return dbConn;
} catch (Exception e)
{
eprintStackTrace();
}
return null;
}
public void closeConnection()
{
try {
dbConnclose();
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
public static void main(String[] srg)
{
// DBConnectionManager DBConnectionManager1 = new DBConnectionManager();
try
{
Systemoutprintln("success");
MssqlConnTest test = new MssqlConnTest();
Connection conn = testgetConnection();
Systemoutprintln("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!
Statement stmt=conncreateStatement();
ResultSet rs=stmtexecuteQuery("select top 1 from DOI");
while(rsnext())
{
Systemoutprint("\tfirst: "+rsgetString(1));
Systemoutprint("\tsecond: "+rsgetString(2));
}
///
}catch(Exception e)
{
eprintStackTrace();
}
}
}
import javaawtBorderLayout;
import javaawtCardLayout;
import javaawtContainer;
import javaawtFont;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingIcon;
import javaxswingImageIcon;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJMenu;
import javaxswingJMenuBar;
import javaxswingJMenuItem;
import javaxswingJPanel;
import javaxswingJToolBar;
import javaxswingSwingConstants;
public class MainFrame extends JFrame implements ActionListener{
InsertPanel ip = null;
SelectPanel sp = null;
JPanel pframe;
JButton jb1,jb2,jb3;
JMenuItem jm11,jm21,jm22,jm23,jm31,jm32,jm41,jm42;
CardLayout clayout;
public MainFrame(String s){
super(s);
JMenuBar mb = new JMenuBar();
thissetJMenuBar(mb);
JMenu m1 = new JMenu("系统");
JMenu m2 = new JMenu("基本信息");
JMenu m3 = new JMenu("成绩");
JMenu m4 = new JMenu("奖惩");
mbadd(m1);
mbadd(m2);
mbadd(m3);
mbadd(m4);
jm11 = new JMenuItem("退出系统");
jm21 = new JMenuItem("输入");
jm22 = new JMenuItem("查询");
jm23 = new JMenuItem("更改");
jm31 = new JMenuItem("输入成绩");
jm32 = new JMenuItem("查询成绩");
jm41 = new JMenuItem("奖励");
jm42 = new JMenuItem("处分");
m1add(jm11);
m2add(jm21);
m2add(jm22);
m2add(jm23);
m3add(jm31);
m3add(jm32);
m4add(jm41);
m4add(jm42);
Icon i1 = new ImageIcon();
Icon i2 = new ImageIcon();
Icon i3 = new ImageIcon();
jb1 = new JButton(i1);
jb1setToolTipText("输入");
jb2 = new JButton(i2);
jb2setToolTipText("查询");
jb3 = new JButton(i3);
jb3setToolTipText("退出");
JToolBar tb = new JToolBar("系统工具");
tbadd(jb1);
tbadd(jb2);
tbadd(jb3);
add(tb,BorderLayoutNORTH);
jm11addActionListener(this);
jm21addActionListener(this);
jm22addActionListener(this);
jb1addActionListener(this);
jb2addActionListener(this);
jb3addActionListener(this);
clayout = new CardLayout();
pframe = new JPanel(clayout);
add(pframe);
JPanel mainp = new JPanel(new BorderLayout());
JLabel mainl = new JLabel("学生信息管理平台",SwingConstantsCENTER);
mainlsetFont(new Font("serif",FontBOLD,30));
mainpadd(mainl);
pframeadd(mainp,"main");
clayoutshow(pframe, "main");
}
public void actionPerformed(ActionEvent e){
if(egetSource() == jm21 || egetSource() == jb1){
if(ip == null){
ip= new InsertPanel();
pframeadd(ip,"insert");
}
clayoutshow(pframe, "insert");
thissetTitle("输入学生信息");
}
else if(egetSource() == jm22 || egetSource() == jb2){
if(sp == null){
sp= new SelectPanel();
pframeadd(sp,"select");
}
clayoutshow(pframe, "select");
thissetTitle("查询学生信息");
}
else if(egetSource() == jm11 || egetSource() == jb3){
Systemexit(0);
}
}
}
第二个:
import javaxswingJFrame;
public class MainTest {
public static void main(String [] args){
MainFrame f = new MainFrame("学生信息管理平台");
fsetSize(400,300);
fsetLocation(350,250);
fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
fsetVisible(true);
}
}
第二个:
import javasqlConnection;
import javasqlDriverManager;
public class MySQLConnection {
static Connection getCon(){
Connection con = null;
try{
ClassforName("commysqljdbcDriver");
con = DriverManagergetConnection("jdbc:mysql://localhost/test","root","123");
}
catch(Exception e){
Systemoutprintln("建立数据库连接遇到异常!");
}
return con;
}
}
第四个:
import javaawtBorderLayout;
import javaawtGridLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJLabel;
import javaxswingJOptionPane;
import javaxswingJPanel;
import javaxswingJTextField;
import javaxswingSwingConstants;
public class SelectPanel extends JPanel implements ActionListener{
JButton jb;
JTextField jt;
JTextField jt1,jt2,jt3,jt4;
public SelectPanel(){
JLabel jl = new JLabel("请输入学号:",SwingConstantsCENTER);
jt = new JTextField();
jb = new JButton("确定");
JPanel jp1 = new JPanel(new GridLayout(1,3));
jp1add(jl);
jp1add(jt);
jp1add(jb);
JLabel j1,j2,j3,j4;
j1 = new JLabel("学号:",SwingConstantsCENTER);
j2 = new JLabel("姓名:",SwingConstantsCENTER);
j3 = new JLabel("性别:",SwingConstantsCENTER);
j4 = new JLabel("年龄:",SwingConstantsCENTER);
jt1 = new JTextField(6);
jt1setEditable(false);
jt2 = new JTextField(6);
jt2setEditable(false);
jt3 = new JTextField(6);
jt3setEditable(false);
jt4 = new JTextField(6);
jt4setEditable(false);
JPanel jp2 = new JPanel(new BorderLayout());
JPanel jp3 = new JPanel(new GridLayout(4,2));
jp2add(new JLabel(""),BorderLayoutNORTH);
jp3add(j1);
jp3add(jt1);
jp3add(j2);
jp3add(jt2);
jp3add(j3);
jp3add(jt3);
jp3add(j4);
jp3add(jt4);
jp2add(jp3);
thissetLayout(new BorderLayout());
add(jp1,BorderLayoutNORTH);
add(jp2);
jbaddActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(egetSource() == jb){
String stuNo = jtgetText()trim();
Student s = new Student();
boolean b = true;
try{
b = sselectByStuNo(stuNo);
}
catch(Exception ex){
Systemoutprintln("查询学生信息遇到异常!");
}
if(b){
jt1setText(sgetStuNo());
jt2setText(sgetName());
jt3setText(sgetGender());
int a = sgetAge();
Integer i = new Integer(a);
jt4setText(itoString());
}
else{
JOptionPaneshowMessageDialog(null, "无此学生!");
}
}
}
}
第五个:
import javaxswingJFrame;
public class SelectTest {
public static void main(String [] args){
JFrame f = new JFrame("查询学生信息");
SelectPanel p = new SelectPanel();
fadd(p);
fsetSize(400,300);
fsetLocation(300,250);
fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
fsetVisible(true);
}
}
第六个:
import javasqlConnection;
import javasqlResultSet;
import javasqlStatement;
public class Student {
String stuNo;
String name;
String gender;
int age;
public Student(){}
public Student(String stuNo,String name,String gender, int age){
thisstuNo = stuNo;
thisname = name;
thisgender = gender;
thisage = age;
}
public String getStuNo(){
return stuNo;
}
public void setStuNo(String stuNo){
thisstuNo = stuNo;
}
public String getName(){
return name;
}
public void setName(String name){
thisname = name;
}
public String getGender(){
return gender;
}
public void setGender(String gender){
thisgender = gender;
}
public int getAge(){
return age;
}
public void setAge(int age){
thisage = age;
}
public boolean insertStudent(){
boolean b = true;
try{
Connection con = MySQLConnectiongetCon();
Statement statement = concreateStatement();
String sql = "insert into student values('" + stuNo + "','" + name +"','" + gender + "'," + age + ")";
sql = new String(sqlgetBytes("gb2312"),"ISO8859_1");
statementexecuteUpdate(sql);
conclose();
}
catch(Exception e){
b = false;
Systemoutprintln("插入数据库遇到异常!");
}
return b;
}
public boolean selectByStuNo(String stuNo)throws Exception{
boolean b = true;
Connection con = MySQLConnectiongetCon();
Statement statement = concreateStatement();
String sql = "select from student where stuNo =" + stuNo;
ResultSet rs = statementexecuteQuery(sql);
if(rs != null && rsnext()){
String no = rsgetString(1);
thissetStuNo(no);
String n = rsgetString(2);
n = new String(ngetBytes("ISO8859_1"),"gb2312");
thissetName(n);
String g = rsgetString(3);
g = new String (ggetBytes("ISO8859_1"),"gb2312");
thissetGender(g);
thissetAge(rsgetInt(4));
b = true;
}
rsclose();
statementclose();
conclose();
return b;
}
}
数据库你自己弄吧,我没时间弄了!初学得多动手哦
你要连的数据库是SQL 还是ORACLE
但是代码都查不多
下面的是连接SQL数据库的代码
你需要先创建个数据库,还有表,表的字段是登陆名和密码
下面的"SA" 是登陆名 "111111" 是密码
ORACLE 和这个查不多
import javasql;//做数据库时候必须要引入的包
import commicrosoftjdbcsqlserverSQLServerDriver;
public class DBFactory {
Connection Conn=null;
PreparedStatement Stmt=null;
ResultSet Rs=null;
String driverName="commicrosoftjdbcsqlserverSQLServerDriver";
String OracleUserName="sa";
String OracleUserPwd="111111";
String ConnUrl="jdbc:sqlserver://localhost:1433;databaseName=news";
public Connection getConnection()
{
try {
ClassforName(driverName);
} catch (ClassNotFoundException ex) {
Systemoutprintln("加载驱动程序有错误");
}
try {
Conn = DriverManagergetConnection(ConnUrl, OracleUserName,OracleUserPwd);
} catch (SQLException ex1) {
Systemoutprint("取得连接的时候有错误,请核对用户名和密码");
}
return Conn;
}
这个是连接ORACLE数据库代码
import javasql;
import oraclejdbcdriverOracleDriver;
public class DBFactory {
Connection Conn=null;
PreparedStatement Stmt=null;
ResultSet Rs=null;
String driverName="oraclejdbcdriverOracleDriver";
String OracleUserName="scott";
String OracleUserPwd="tiger";
String ConnUr1="jdbc:oracle:thin:@locahost:1521:Ora";
public Connection getConnection()
{
try {
ClassforName(driverName);
} catch (ClassNotFoundException ex) {
Systemoutprintln("加载驱动程序有错误");
}
try {
Conn = DriverManagergetConnection(ConnUr1, OracleUserName,OracleUserPwd);
} catch (SQLException ex1) {
Systemoutprint("取得连接时有错误,请核对用户名和密码");
}
return Conn;
}
希望能追加分数谢谢!
用JDBC的话就是
ClassforName("commysqljdbcDriver"); //加载驱动
Connection con = DriverManagergetConnection("jdbc:mysql://localhost:3306/表空间名",
"用户名", "密码"); //获取连接
Statement stmt = concreateStatement();
stmtexecuteUpdate(" *** 作数据库");
那个不好意思,我来当坏人吧,没人会鸟你的,这世界好人没人想的那么多,最简单的自己在百度搜一个,但是一般数据库或者jdk版本会不兼容,还有你的悬赏太少了,根本没有人会来回答的,我建议你还自己堆起来吧,这个不难,只是堆代码而已,现在eclipse都可以拖放swing部件了
以上就是关于临近毕业,用java做了个学生信息管理系统,需要用到sql数据库,请问怎么连接,急用,谢谢各位全部的内容,包括:临近毕业,用java做了个学生信息管理系统,需要用到sql数据库,请问怎么连接,急用,谢谢各位、急求java学生信息管理系统源代码,带有连接数据库的,万分感谢、(高分)急求连接数据库的JAVA学生信息管理系统源代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)